home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / ddx-mips.zip / MIPSCOLO.C < prev    next >
C/C++ Source or Header  |  1992-07-31  |  6KB  |  240 lines

  1. /*
  2.  * $XConsortium$
  3.  *
  4.  * Copyright 1991 MIPS Computer Systems, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of MIPS not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  MIPS makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * MIPS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL MIPS
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  */
  23. #ident    "$Header: mipsColor.c,v 1.8 91/10/12 16:22:35 dd Exp $"
  24.  
  25. #include <sys/types.h>
  26.  
  27. #include "X.h"
  28. #include "Xproto.h"
  29. #include "scrnintstr.h"
  30. #include "colormapst.h"
  31. #include "windowstr.h"
  32. #include "input.h"
  33.  
  34. #include "mips.h"
  35. #include "mipsFb.h"
  36.  
  37. int
  38. mipsListInstalledColormaps(pScreen, pmaps)
  39.     ScreenPtr pScreen;
  40.     Colormap *pmaps;
  41. {
  42.     MipsScreenPtr pm = MipsScreenToPriv(pScreen);
  43.  
  44.     /* By the time we are processing requests, we can guarantee that
  45.      * there is always a colormap installed */
  46.  
  47.     *pmaps = pm->InstalledMap->mid;
  48.     return 1;
  49. }
  50.  
  51. void
  52. mipsStoreColors(pmap, ndef, pdefs)
  53.     ColormapPtr pmap;
  54.     int ndef;
  55.     xColorItem *pdefs;
  56. {
  57.     MipsScreenPtr pm = MipsScreenToPriv(pmap->pScreen);
  58.     void (*WriteCMap)();
  59.     unsigned char rgb[256 * 3];
  60. #ifndef X11R4
  61.     xColorItem expanddefs[256];
  62. #endif
  63.  
  64.     if (pmap != pm->InstalledMap)
  65.         return;
  66.  
  67. #ifndef X11R4
  68.     if ((pmap->pVisual->class | DynamicClass) == DirectColor) {
  69.         ndef = cfbExpandDirectColors(pmap, ndef, pdefs, expanddefs);
  70.         pdefs = expanddefs;
  71.     }
  72. #endif /* !X11R4 */
  73.  
  74.     if (!(WriteCMap = pm->WriteCMap))
  75.         return;
  76.  
  77.     while (ndef > 0) {
  78.         int index = pdefs->pixel;
  79.         int prev = index;
  80.         int count = 0;
  81.         unsigned char *out = rgb;
  82.  
  83.         do {
  84.             out[0] = pdefs->red >> 8;
  85.             out[1] = pdefs->green >> 8;
  86.             out[2] = pdefs->blue >> 8;
  87.             out += 3;
  88.             count++;
  89.         } while (--ndef > 0 && (++pdefs)->pixel == ++prev);
  90.  
  91.         (*WriteCMap)(pm, index, count, rgb);
  92.     }
  93. }
  94.  
  95. void
  96. mipsInstallColormap(pmap)
  97.     ColormapPtr pmap;
  98. {
  99.     ScreenPtr pScreen = pmap->pScreen;
  100.     int index = pScreen->myNum;
  101.     MipsScreenPtr pm = MipsScreenNumToPriv(index);
  102.     ColormapPtr oldpmap = pm->InstalledMap;
  103.     VisualPtr pVisual;
  104.     Entry *in;
  105.     int i, count;
  106.     unsigned char *out, rgb[256 * 3];
  107.  
  108.     if (pmap == oldpmap)
  109.         return;
  110.  
  111.     /*
  112.      * Uninstall existing map. No hardware changes required, just
  113.      * notify all interested parties.
  114.      */
  115.     if (oldpmap != (ColormapPtr) None)
  116.         WalkTree(oldpmap->pScreen, TellLostMap,
  117.             (pointer) &oldpmap->mid);
  118.  
  119.     /* Install new map */
  120.     pm->InstalledMap = pmap;
  121.  
  122.     pVisual = pmap->pVisual;
  123.     out = rgb;
  124.  
  125. #ifndef X11R4
  126.     if ((pVisual->class | DynamicClass) == DirectColor) {
  127.         count = 256;
  128.         for (i = 0; i < count; i++) {
  129.             out[0] = pmap->red[(i & pVisual->redMask) >>
  130.                 pVisual->offsetRed].co.local.red >> 8;
  131.             out[1] = pmap->green[(i & pVisual->greenMask) >>
  132.                 pVisual->offsetGreen].co.local.green >> 8;
  133.             out[2] = pmap->blue[(i & pVisual->blueMask) >>
  134.                 pVisual->offsetBlue].co.local.blue >> 8;
  135.             out += 3;
  136.         }
  137.     }
  138.     else
  139. #endif /* !X11R4 */
  140.     {
  141.         count = pVisual->ColormapEntries;
  142.         in = pmap->red;
  143.         for (i = 0; i < count; i++) {
  144.             if (in->fShared) {
  145.                 out[0] = in->co.shco.red->color >> 8;
  146.                 out[1] = in->co.shco.green->color >> 8;
  147.                 out[2] = in->co.shco.blue->color >> 8;
  148.             }
  149.             else {
  150.                 out[0] = in->co.local.red >> 8;
  151.                 out[1] = in->co.local.green >> 8;
  152.                 out[2] = in->co.local.blue >> 8;
  153.             }
  154.             in++;
  155.             out += 3;
  156.         }
  157.     }
  158.  
  159.     if (pm->WriteCMap)
  160.         (*pm->WriteCMap)(pm, 0, count, rgb);
  161.  
  162.     WalkTree(pScreen, TellGainedMap, (pointer) &pmap->mid);
  163. }
  164.  
  165. void
  166. mipsUninstallColormap(pmap)
  167.     ColormapPtr pmap;
  168. {
  169.     ScreenPtr pScreen = pmap->pScreen;
  170.     MipsScreenPtr pm = MipsScreenToPriv(pScreen);
  171.  
  172.     if (pmap != pm->InstalledMap)
  173.         return;
  174.  
  175.     /* Install default map */
  176.     pmap = (ColormapPtr) LookupIDByType(pScreen->defColormap,
  177.         RT_COLORMAP);
  178.     (*pScreen->InstallColormap)(pmap);
  179. }
  180.  
  181.  
  182. /*
  183.  * taken from X11R5 ddx/dec/ws/cfbinit.c -- thank you DEC!
  184.  */
  185. static void
  186. colorNameToColor(index, name, red, green, blue)
  187.     int index;
  188.     char *name;
  189.     unsigned short *red, *green, *blue;
  190. {
  191.     /* hex color */
  192.     if (name[0] == '#') {
  193.         int value;
  194.         if (sscanf(&name[1], "%6x", &value) == 1) {
  195.             *blue = value << 8;
  196.             *green = (value >>= 8) << 8;
  197.             *red = (value >>= 8) << 8;
  198.         }
  199.     }
  200.     /* named color */
  201.     else
  202.         (void) OsLookupColor(index, name, strlen(name),
  203.             red, green, blue);
  204. }
  205.  
  206. /* based on cfb/cfbcmap.c:cfbCreateDefColormap() */
  207. Bool
  208. mipsCreateDefColormap(index, pScreen, white, black)
  209.     int index;
  210.     ScreenPtr pScreen;
  211.     char *white, *black;
  212. {
  213.     VisualPtr pVisual;
  214.     ColormapPtr cmap;
  215.     unsigned short red, green, blue;
  216.  
  217.     for (pVisual = pScreen->visuals;
  218.         pVisual->vid != pScreen->rootVisual;
  219.         pVisual++)
  220.          /* nothing */ ;
  221.  
  222.     if (CreateColormap(pScreen->defColormap, pScreen, pVisual, &cmap,
  223.         (pVisual->nplanes == 1 || (pVisual->class & DynamicClass)) ?
  224.             AllocNone : AllocAll, 0))
  225.         return FALSE;
  226.  
  227.     red = green = blue = 0xffff;
  228.     colorNameToColor(index, white, &red, &green, &blue);
  229.     if (AllocColor(cmap, &red, &green, &blue, &pScreen->whitePixel, 0))
  230.         return FALSE;
  231.  
  232.     red = green = blue = 0;
  233.     colorNameToColor(index, black, &red, &green, &blue);
  234.     if (AllocColor(cmap, &red, &green, &blue, &pScreen->blackPixel, 0))
  235.         return FALSE;
  236.  
  237.     (*pScreen->InstallColormap) (cmap);
  238.     return TRUE;
  239. }
  240.